package me.ezitku.base.extensions

import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import androidx.fragment.app.Fragment
import android.widget.Toast
import com.ug_project.objects.isNotNull
import me.ezitku.base.utils.FileUtils
import java.io.*
import java.text.SimpleDateFormat
import java.util.*


val SDCard: String
    get() = Environment.getExternalStorageDirectory().absolutePath + File.separator


val baseAppFile: File
    get() {
        var path = File("${SDCard}MunberIC/")
        if (!path.exists())
            path.mkdirs()
        return path
    }

val baseAppPath: String
    get() = baseAppFile.absolutePath + "/"


var createPath: String
    get() = ""
    set(value) {
        var file = File(value)
        if (!file.exists())
            file.mkdirs()
    }

val screenRecoderPath: String
    get() {
        createPath = baseAppPath + "ScreenRecoder"
        return baseAppPath + "ScreenRecoder/"
    }

val screenCapturePath: String
    get() {
        createPath = baseAppPath + "ScreenCapture"
        return baseAppPath + "ScreenCapture/"
    }

val audioRecoderPath: String
    get() {
        createPath = baseAppPath + "AudioRecoder"
        return baseAppPath + "AudioRecoder/"
    }


/**
 * قىممىتى يازماقچى بولغان ھۆججەتنىڭ ئىسمى بولىدۇ
 * نۆۋەتتىكى string بولسا يازماقچى بولغان مەزمۇن بولىدۇ
 */
var String.write: String
    get() = ""
    set(value) {
        var file = File(SDCard + value)
        if (!file.exists())
            file.createNewFile()
        file.writeText(this)
    }
/**
 * نۆۋەتتىكى String ئوقۇماقچى بولغان ھۆججەتنىڭ ئىسمى بولىدۇ
 */
val String.read: String
    get() = File(SDCard + this).readText()


/**
 *ئەپنىڭ data مۇندەرىجىسى ئاستىدىكى database نىڭ ئىچىدىكى مەلۇم بىر ھۆججەتكە ئېرىشىشى
 */
fun Fragment.getDatabasePath(fileName: String): String {
    return context!!.getDatabasePath(fileName).absolutePath
}

fun Fragment.getDatabaseFile(fileName: String): File {
    return context!!.getDatabasePath(fileName)
}

fun Context.getDatabasePath(fileName: String): String {
    return getDatabasePath(fileName).absolutePath
}


/**
 * ھۆججەتنىڭ قۇرۇلغان ۋاقتى
 */
val File.createTime: String
    get() {
        val filePath = absolutePath
        var createTime = ""
        try {
            val p = Runtime.getRuntime().exec("cmd /C dir $filePath/tc")
            val `is` = p.inputStream
            val br = BufferedReader(InputStreamReader(`is`))
            while (true) {
                if (br.readLine() != null) {
                    createTime = br.readLine().substring(0, 17)
                }
                break
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return createTime
    }


/**
 *ھۆججەتنىڭ ئاخىرقى ئۆزگەرگەن ۋاقتى
 */
val File.lastChangeTime: String
    get() {
        val cal = Calendar.getInstance()
        val time = lastModified()
        lastModified()
        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        cal.timeInMillis = time
        return formatter.format(cal.time)
    }


fun File.writeBitmap(bitmap: Bitmap) {
    try {
        val out = FileOutputStream(this)
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out)
        out.flush()
        out.close()
    } catch (e: FileNotFoundException) {
        throw Exception("writeBitmap Err : FileNotFoundException")
    } catch (e: IOException) {
        throw Exception("writeBitmap Err : IOException")
    }
}

fun Activity.choseFile(REQUEST_CODE: Int) {
    val intent = Intent(Intent.ACTION_GET_CONTENT)
    intent.type = "*/*"
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    try {
        startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), REQUEST_CODE)
    } catch (ex: android.content.ActivityNotFoundException) {
        Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show()
    }
}

fun Activity.getChoseFile(requestCode: Int, resultCode: Int, data: Intent, REQUEST_CODE: Int, action: (File) -> Unit) {
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        var res = FileUtils.getPath(this, data.data)
        println("---------------:   " + res)
        action.invoke(File(res))

    }

}


